package com.mini.framework.communicate.mina;

import java.net.InetSocketAddress;
import java.util.logging.Logger;

import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import com.mini.framework.communicate.mina.adapter.MsgHandlerAdapter;
import com.mini.framework.communicate.mina.protocol.ProtocolCodeFactory;

public class Mini {

	
	private Logger log = Logger.getLogger("Mini");

	/**
	 * 程序启动.
	 * 
	 */
	public void start(ServerConfig configs) {
		try {
			log.info("正在启动mini.... ");
			for(Config conf:configs.getConfigList()){
				NioSocketAcceptor acceptor = new NioSocketAcceptor();
				IoFilter codeFilter = new ProtocolCodecFilter(new ProtocolCodeFactory(conf.getCodeFactory()));
				acceptor.getFilterChain().addLast("codec", codeFilter);
				acceptor.getFilterChain().addLast("logger", new LoggingFilter());
				acceptor.setHandler(new MsgHandlerAdapter(conf.getHandlerFactory()));
				acceptor.bind(new InetSocketAddress(conf.getServerPort()));
			}
			log.info("启动mini完毕....");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
